// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>
#include <string>
#include <vector>

#include <nebula/core/extension_type.h>
#include <turbo/base/macros.h>

namespace nebula {

class TURBO_EXPORT UuidArray : public ExtensionArray {
 public:
  using ExtensionArray::ExtensionArray;
};

class TURBO_EXPORT UuidType : public ExtensionType {
 public:
  UuidType() : ExtensionType(fixed_size_binary(16)) {}

  std::string extension_name() const override { return "uuid"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "uuid-serialized"; }
};

class TURBO_EXPORT SmallintArray : public ExtensionArray {
 public:
  using ExtensionArray::ExtensionArray;
};

class TURBO_EXPORT TinyintArray : public ExtensionArray {
 public:
  using ExtensionArray::ExtensionArray;
};

class TURBO_EXPORT ListExtensionArray : public ExtensionArray {
 public:
  using ExtensionArray::ExtensionArray;
};

class TURBO_EXPORT SmallintType : public ExtensionType {
 public:
  SmallintType() : ExtensionType(int16()) {}

  std::string extension_name() const override { return "smallint"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "smallint"; }
};

class TURBO_EXPORT TinyintType : public ExtensionType {
 public:
  TinyintType() : ExtensionType(int8()) {}

  std::string extension_name() const override { return "tinyint"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "tinyint"; }
};

class TURBO_EXPORT ListExtensionType : public ExtensionType {
 public:
  ListExtensionType() : ExtensionType(list(int32())) {}

  std::string extension_name() const override { return "list-ext"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "list-ext"; }
};

class TURBO_EXPORT DictExtensionType : public ExtensionType {
 public:
  DictExtensionType() : ExtensionType(dictionary(int8(), utf8())) {}

  std::string extension_name() const override { return "dict-extension"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "dict-extension-serialized"; }
};

// A minimal extension type that does not error when passed blank extension information
class TURBO_EXPORT MetadataOptionalExtensionType : public ExtensionType {
 public:
  MetadataOptionalExtensionType() : ExtensionType(null()) {}
  std::string extension_name() const override { return "metadata.optional"; }
  std::string serialize() const override { return ""; }
  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override {
    return nullptr;
  }
  bool ExtensionEquals(const ExtensionType& other) const override {
    return other.extension_name() == extension_name();
  }
  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized_data) const override {
    return std::make_shared<MetadataOptionalExtensionType>();
  }
};

class TURBO_EXPORT Complex128Array : public ExtensionArray {
 public:
  using ExtensionArray::ExtensionArray;
};

class TURBO_EXPORT Complex128Type : public ExtensionType {
 public:
  Complex128Type()
      : ExtensionType(STRUCT({::nebula::field("real", float64(), /*nullable=*/false),
                               ::nebula::field("imag", float64(), /*nullable=*/false)})) {}

  std::string extension_name() const override { return "complex128"; }

  bool ExtensionEquals(const ExtensionType& other) const override;

  std::shared_ptr<Array> make_array(std::shared_ptr<ArrayData> data) const override;

  turbo::Result<std::shared_ptr<DataType>> deserialize(
      std::shared_ptr<DataType> storage_type,
      const std::string& serialized) const override;

  std::string serialize() const override { return "complex128-serialized"; }
};

TURBO_EXPORT
std::shared_ptr<DataType> uuid();

TURBO_EXPORT
std::shared_ptr<DataType> smallint();

TURBO_EXPORT
std::shared_ptr<DataType> tinyint();

TURBO_EXPORT
std::shared_ptr<DataType> list_extension_type();

TURBO_EXPORT
std::shared_ptr<DataType> dict_extension_type();

TURBO_EXPORT
std::shared_ptr<DataType> complex128();

TURBO_EXPORT
std::shared_ptr<Array> ExampleUuid();

TURBO_EXPORT
std::shared_ptr<Array> ExampleSmallint();

TURBO_EXPORT
std::shared_ptr<Array> ExampleTinyint();

TURBO_EXPORT
std::shared_ptr<Array> ExampleDictExtension();

TURBO_EXPORT
std::shared_ptr<Array> ExampleComplex128();

TURBO_EXPORT
std::shared_ptr<Array> MakeComplex128(const std::shared_ptr<Array>& real,
                                      const std::shared_ptr<Array>& imag);

// A RAII class that registers an extension type on construction
// and unregisters it on destruction.
class TURBO_EXPORT ExtensionTypeGuard {
 public:
  explicit ExtensionTypeGuard(const std::shared_ptr<DataType>& type);
  explicit ExtensionTypeGuard(const DataTypeVector& types);
  ~ExtensionTypeGuard();
  TURBO_DEFAULT_MOVE_AND_ASSIGN(ExtensionTypeGuard);

 protected:
  TURBO_DISALLOW_COPY_AND_ASSIGN(ExtensionTypeGuard);

  std::vector<std::string> extension_names_;
};

}  // namespace nebula
